package customize

import (
	"errors"
	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/customize"
	customizeReq "gitcode.com/G-YT/baoyou/server/model/customize/request"
	"gitcode.com/G-YT/baoyou/server/utils"
	"gorm.io/gorm"
)

type DrugService struct{}

// CreateDrug 创建病症
func (drugService *DrugService) CreateDrug(drug *customize.Drug) (err error) {
	err = global.GVA_DB.Create(drug).Error
	return err
}

func (drugService *DrugService) DeleteDrug(ID string) (err error) {
	var count int64
	err = global.GVA_DB.Find(&customize.Drug{}, "parent_id = ?", ID).Count(&count).Error
	if count > 0 {
		return errors.New("此节点存在子节点不允许删除")
	}
	if err != nil {
		return err
	}
	err = global.GVA_DB.Delete(&customize.Drug{}, "id = ?", ID).Error
	return err
}

// DeleteDrugByIds 批量删除
func (drugService *DrugService) DeleteDrugByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]customize.Drug{}, "id in ?", IDs).Error
	return err
}

// UpdateDrug 更新病症
func (drugService *DrugService) UpdateDrug(drugs customize.Drug) (err error) {
	err = global.GVA_DB.Model(&customize.Drug{}).Where("id = ?", drugs.ID).Updates(&drugs).Error
	return err
}

// GetDrug 根据ID获取病症
func (drugService *DrugService) GetDrug(ID string) (drug customize.Drug, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&drug).Error
	return
}

// GetDrugListByParentID 根据病症类型ID获取病症列表
func (drugService *DrugService) GetDrugListByParentID(IDs []string) (list []customize.Drug, err error) {
	err = global.GVA_DB.Where("parent_id IN (?)", IDs).Find(&list).Error
	return
}

// GetDrugTypeList 获取病症类型列表
func (drugService *DrugService) GetDrugTypeList() (list []customize.Drug, err error) {
	err = global.GVA_DB.Where("parent_id = 0").Find(&list).Error
	return
}

// GetDrugInfoList 获取病症列表(树形)
func (drugService *DrugService) GetDrugInfoList() (list []*customize.Drug, err error) {
	// 创建db
	db := global.GVA_DB.Model(&customize.Drug{})
	var drugs []*customize.Drug

	err = db.Find(&drugs).Error

	return utils.BuildTree(drugs), err
}

// SetDrugArticle 设置病症文章关系
func (drugService *DrugService) SetDrugArticle(req customizeReq.DrugArticleReq) (err error) {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("drug_id = ?", req.DrugID).Delete(&customize.DrugArticle{}).Error; err != nil {
			return err
		}
		var drugIDArticles []customize.DrugArticle
		for _, richText := range req.RichTexts {
			drugIDArticles = append(drugIDArticles, customize.DrugArticle{
				DrugID:    req.DrugID,
				ArticleID: richText,
				Type:      "richText",
			})
		}
		for _, audio := range req.Audios {
			drugIDArticles = append(drugIDArticles, customize.DrugArticle{
				DrugID:    req.DrugID,
				ArticleID: audio,
				Type:      "audio",
			})
		}
		for _, video := range req.Videos {
			drugIDArticles = append(drugIDArticles, customize.DrugArticle{
				DrugID:    req.DrugID,
				ArticleID: video,
				Type:      "video",
			})
		}
		if len(drugIDArticles) == 0 {
			return nil
		}
		return tx.Create(&drugIDArticles).Error
	})
}

// GetDrugArticle 获取病症文章关系
func (drugService *DrugService) GetDrugArticle(drugID uint) (list []customize.DrugArticle, err error) {
	err = global.GVA_DB.Where("drug_id = ?", drugID).Find(&list).Error
	return
}
